package com.wj.hourupdate;

import com.wj.hourupdate.datasource.SettingData;
import com.wj.hourupdate.datasource.SettingDatasource;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class SettingActivity extends Activity {
	private EditText txtIp;
	private EditText txtPort;
	
	private SettingDatasource settingSource;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		txtIp = (EditText) findViewById(R.id.editText1);
		txtPort = (EditText) findViewById(R.id.editText2);
		
		settingSource = new SettingDatasource(SettingActivity.this);
		
		getSetting();
	}

	public void onSaveClicked(final View v){
		if(!txtIp.getText().toString().equals("") && 
				!txtPort.getText().toString().equals("")){
			settingSource.open();
			settingSource.saveSetting(txtIp.getText().toString(), txtPort.getText().toString());
			settingSource.close();
			
			getSetting();
			
			SettingActivity.this.finish();
		}
	}
	
	private void getSetting(){
		settingSource.open();
		SettingData setData = settingSource.getSetting();
		txtIp.setText(setData.getIp());
		txtPort.setText(setData.getPort());
		settingSource.close();
		
		GlobalVar.IP = setData.getIp();
		GlobalVar.PORT = setData.getPort();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting, menu);
		return true;
	}

}
